﻿using System.ComponentModel.Composition;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System;

namespace NColony.Syndication.Receiver.Contracts
{
    /// <summary>
    /// Base feed for syndication feeds.
    /// </summary>
    [Export(typeof(ISyndicationFeed))]
    [PartCreationPolicy(CreationPolicy.NonShared)]
    public class BaseFeed : ISyndicationFeed
    {
        private readonly ObservableCollection<ISyndicationFeedItem> items;

        private string title;

        /// <summary>
        /// Initializes a new instance of the <see cref="BaseFeed"/> class.
        /// </summary>
        public BaseFeed()
        {
            this.items = new ObservableCollection<ISyndicationFeedItem>();
        }

        /// <summary>
        /// Gets or sets the title.
        /// </summary>
        /// <value>The title.</value>
        public string Title
        {
            get
            {
                return this.title;
            }

            set
            {
                if (value == null) throw new ArgumentNullException("value", "Title cannot be null");

                this.OnPropertyChanging("Title");
                this.title = value;
                this.OnPropertyChanged("Title");
            }
        }

        /// <summary>
        /// Gets the items.
        /// </summary>
        /// <value>The items.</value>
        public ObservableCollection<ISyndicationFeedItem> Items
        {
            get
            {
                return this.items;
            }
        }

        /// <summary>
        /// Occurs when a property value is changing.
        /// </summary>
        public event PropertyChangingEventHandler PropertyChanging;

        /// <summary>
        /// Occurs when a property value changes.
        /// </summary>
        public event PropertyChangedEventHandler PropertyChanged;

        /// <summary>
        /// Called when [property changing].
        /// </summary>
        /// <param name="propertyName">Name of the property.</param>
        protected void OnPropertyChanging(string propertyName)
        {
            if (this.PropertyChanging != null)
            {
                this.PropertyChanging(this, new PropertyChangingEventArgs(propertyName));
            }
        }

        /// <summary>
        /// Called when [property changed].
        /// </summary>
        /// <param name="propertyName">Name of the property.</param>
        protected void OnPropertyChanged(string propertyName)
        {
            if (this.PropertyChanged != null)
            {
                this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }
    }
}